package mhcs.client;

import java.util.Iterator;
import java.util.TreeSet;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.storage.client.Storage;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
/**
 * 
 * Manages Saving and Loading of configurations.
 * @author tristan markert
 *
 */
public class SaveLoadConfig extends Composite {
	/**
	 * Where retrieved storage is stored so that load can access values.
	 */
	private Storage moduleStore = null;
	/**
	 * Horizontal panel that wraps the buttons.
	 */
	private HorizontalPanel vp;
	
	/**
	 * Creates an instance of the save and load buttons and accesses 
	 * local memory from previous sessions.
	 */
	public SaveLoadConfig() {
		
		moduleStore = Storage.getLocalStorageIfSupported();
		
		vp = new HorizontalPanel();
		Button saveButton = new Button("Save Modules", new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				saveConfiguration();
				
			}
			
		});
		Button loadButton = new Button("Load Modules", new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				loadConfiguration();
				
			}
			
		});
		
		vp.add(saveButton);
		vp.add(loadButton);
		//final line of constructor
		initWidget(vp);
	}
	/**
	 * Method that saves all modules logged into local storage
	 * and chosen configurations.
	 */
	private void saveConfiguration() {
		TreeSet<Module> tempTree = MHCS.dataSet.getTree();
		Iterator<Module> iter = tempTree.iterator();
		String sAll = "[";
		
		MHCS.soundOutput.playSound7();
		
		while (iter.hasNext()) {
			Module next = iter.next();
			Integer codeInt = next.getSerial();
			String code = codeInt.toString();
			String status = next.getStatus();
			Integer turnNum = next.getRotations();
			String turns = turnNum.toString();
			Integer xPosition = next.getX();
			String xPos = xPosition.toString();
			Integer yPosition = next.getY();
			String yPos = yPosition.toString();
			
			sAll = sAll + "{code:" + code + ",status:" + "\"" + status + "\"" 
			+ ",turns:" + turns + ",X:" + xPos + ",Y:" + yPos + "}";
			if (iter.hasNext()) {
				sAll = sAll + ",";
			}
		}
		sAll = sAll + "]";
		
		moduleStore = Storage.getLocalStorageIfSupported();
		Integer configNum = MHCS.dataSet.getConfiguration();
		 if (moduleStore != null) { 
			 moduleStore.setItem("config1", sAll);
			 moduleStore.setItem("chosenConfig", configNum.toString());
		 }  

	}
	/**
	 * Loads any modules that have been saved and loads them back
	 * into the map. Also sets previously chosen configurations.
	 */
	public void loadConfiguration() {
		MHCS.dataSet.clear();
		
		moduleStore = Storage.getLocalStorageIfSupported(); 
		String sConfigOne = moduleStore.getItem("config1"); 
		 
		MHCS.soundOutput.playSound8();
		
		 JSONArray jA = 
		 (JSONArray) JSONParser.parseLenient(sConfigOne); 
		 
		 JSONNumber jN; 
		 JSONString jS; 
		 double serial;
		 double turn;
		 double xVal;
		 double yVal;
		 String stat;
		 
		 for (int i = 0; i < jA.size(); i++) {
			 JSONObject jO = (JSONObject) jA.get(i); 
			 jN = (JSONNumber) jO.get("code"); 
			 serial = jN.doubleValue();
			 jS = (JSONString) jO.get("status"); 
			 stat = jS.stringValue();  
			 jN = (JSONNumber) jO.get("turns"); 
			 turn = jN.doubleValue(); 
			 jN = (JSONNumber) jO.get("X"); 
			 xVal = jN.doubleValue();  
			 jN = (JSONNumber) jO.get("Y"); 
			 yVal = jN.doubleValue();
			 try {
				MHCS.dataSet.soundFlag = true;
				MHCS.dataSet.add((int) serial, stat, (int) turn, 
						(int) xVal, (int) yVal);
				MHCS.dataSet.soundFlag = false;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String configStr = moduleStore.getItem("chosenConfig");
		int configNum = Integer.valueOf(configStr);
		MHCS.dataSet.setConfiguration(configNum);
		
	}
	
	
}
